home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / xshogi11.lha / xshogi-1.1 / parser.bison < prev    next >
Text File  |  1993-04-13  |  3KB  |  148 lines

  1.  
  2. %{
  3.  
  4. /*
  5.  * parser.c - C source for Xshogi
  6.  *
  7.  * Copyright (c) 1993 Matthias Mutz
  8.  *
  9.  * XShogi is based on XBoard 2.0
  10.  *
  11.  * Copyright (c) 1992 Free Software Foundation
  12.  *
  13.  * This file is part of XShogi.
  14.  *
  15.  * XShogi is free software; you can redistribute it and/or modify
  16.  * it under the terms of the GNU General Public License as published by
  17.  * the Free Software Foundation; either version 1, or (at your option)
  18.  * any later version.
  19.  *
  20.  * XShogi is distributed in the hope that it will be useful,
  21.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23.  * GNU General Public License for more details.
  24.  *
  25.  * You should have received a copy of the GNU General Public License
  26.  * along with XShogi; see the file COPYING.  If not, write to
  27.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  28.  */
  29.  
  30. #include "xshogi.h"
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34.                   
  35. enum { False, True };
  36.  
  37. static void yyerror();
  38.            
  39. static ChessMove move_type;
  40. static int      from_x, from_y, to_x, to_y;
  41. static char      piece;
  42.  
  43. char currentMoveString[MSG_SIZ];
  44.  
  45. FILE *outfile = (FILE *)0;
  46.  
  47. extern FILE     *gameFileFP, *toFirstProgFP;
  48. extern int     currentMove;
  49. extern char      moveList[MAX_MOVES][MOVE_LEN];
  50.  
  51. extern void SendToProgram P((char *message, FILE *fp));
  52. extern void MakeMove P((ChessMove *move_type, int from_x, int from_y, int to_x, int to_y));
  53.  
  54. %}
  55.  
  56. %start goal
  57.  
  58. %token PROMOTE DROPS PIECE SQUARE NUMBER CROSS COLON
  59.       
  60. %union { int val; char* string; }
  61.  
  62. %%         
  63.  
  64.  goal:
  65.    CROSS move_list
  66.  ;
  67.  
  68.  move_list:
  69.   | move_list number promoted move
  70.  ;
  71.  
  72.  number:
  73.   | NUMBER COLON
  74.  ;
  75.  
  76.  promoted:
  77.   | PROMOTE
  78.  ;
  79.  
  80.  move:
  81.    SQUARE  { from_x = '9' - $<string>1[0]; 
  82.          from_y = 'i' - $<string>1[1];
  83.          strcpy(currentMoveString,$<string>1); }
  84.    SQUARE  { to_x = '9' - $<string>3[0]; 
  85.          to_y = 'i' - $<string>3[1];
  86.          strcat(currentMoveString,$<string>3); }
  87.    { move_type = NormalMove; }
  88.    promotion
  89.     { 
  90.       SendToProgram(currentMoveString, toFirstProgFP);
  91.       strcpy(moveList[currentMove], currentMoveString);
  92.       MakeMove(&move_type, from_x, from_y, to_x, to_y);
  93.     }
  94.   |
  95.    PIECE   { piece = $<string>1[0]; strcpy(currentMoveString,$<string>1); }
  96.    DROPS   { strcat(currentMoveString,"*"); }
  97.    SQUARE  { to_x = '9' - $<string>5[0]; 
  98.          to_y = 'i' - $<string>5[1];
  99.          strcat(currentMoveString,$<string>5); }
  100.     { 
  101.       move_type = (BLACK_ON_MOVE ? BlackDrop : WhiteDrop );
  102.       switch ( piece ) {
  103.         case 'P': from_x = 81; break;
  104.         case 'L': from_x = 82; break;
  105.         case 'N': from_x = 83; break;
  106.         case 'S': from_x = 84; break;
  107.         case 'G': from_x = 85; break;
  108.         case 'B': from_x = 86; break;
  109.         case 'R': from_x = 87; break;
  110.         case 'K': from_x = 88; break;
  111.         default:  from_x = -1;
  112.       };
  113.       from_y = from_x;
  114.       SendToProgram(currentMoveString, toFirstProgFP);
  115.       strcpy(moveList[currentMove], currentMoveString);
  116.       MakeMove(&move_type, from_x, from_y, to_x, to_y);
  117.     }
  118.  
  119.  ;         
  120.  
  121.  promotion:
  122.    | PROMOTE 
  123.     { move_type = (BLACK_ON_MOVE ? BlackPromotion : WhitePromotion ); 
  124.       strcat(currentMoveString,"+"); }
  125.  ;
  126.  
  127. %%
  128.  
  129.  
  130.  
  131. #include "scanner.c"
  132.  
  133.  
  134. static void yyerror (errmsg)
  135. char *errmsg;
  136. { fprintf(stderr,"parse error in game file line %d column %d : %s\n",lines,cols,errmsg); 
  137.   exit(-1); 
  138. }
  139.        
  140.  
  141. void parseGameFile ()
  142.   yyin = gameFileFP;
  143.   outfile = stderr;
  144.   yyparse();
  145. }
  146.  
  147.